#include<map>

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
          map<char,int> charMap;
        int oldSize,old,temp,max;
        map<char,int>::iterator p;
        oldSize = old = temp = max =0;
        for (int i = 0; i < s.length(); i++)
        {
          //oldSize=charMap.size();
          p=charMap.find(s[i]);
          if(p==charMap.end())
          {
            charMap[s[i]] = i;
              //if(oldSize!=charMap.size())
            temp++;
          }
          else
          {
            old=p->second;
            p->second = i;
            temp =(temp<i-old?temp+1: i-old);
          }
          if(temp>max) max=temp;
        }
        return max;
    }
};
